package com.xiangdao.controller;

import com.xiangdao.dao.UserDao;
import com.xiangdao.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class PInformationController {
    @Autowired
    UserDao userDao;
    @RequestMapping(path = "/pInformation/{name}",method = RequestMethod.GET)
    public String displayPhformation(HttpSession session, Model model,@PathVariable String name) {
            User userById = userDao.getUserByUsername(name);
            model.addAttribute("u",userById);
        return "/basic_information";
    }
    @RequestMapping(path = "/updateUser", method = RequestMethod.POST)
    public View addShareProduct(@RequestParam("birthday") String birthday,
                                @RequestParam("city") String city, @RequestParam("signature") String signature,
                                @RequestParam("time") String time, @RequestParam("college") String college,
                                @RequestParam("cpProfile") String cpProfile, @RequestParam("ruserid") String user_name,
                                @RequestParam("file") MultipartFile pic,@RequestParam("rsex") String sex,HttpServletRequest request)
    {
        User userByUsername = userDao.getUserByUsername(user_name);
        String fn;
        File targetFile = null;

//获取文件在服务器的储存位置
            String path = "D:\\ASUS\\IdeaProjects\\xiangdao_web\\xiangdao\\src\\main\\resources\\static\\images\\new";
            File filePath = new File(path);
            if (!filePath.exists() && !filePath.isDirectory()) {
                filePath.mkdir();
            }
            //获取原始文件名称(包含格式)
            String originalFileName = pic.getOriginalFilename();
            //获取文件类型，以最后一个`.`为标识
            assert originalFileName != null;
            //获取文件名称（不包含格式）
//        String name = originalFileName.substring(originalFileName.lastIndexOf("/") + 1);
            String fileName = UUID.randomUUID() + ".jpg";
            //在指定路径下创建一个文件
            targetFile = new File(path, fileName);
        if(targetFile.length()>1){
            fn=targetFile.getName();
        }else {
            fn=userByUsername.getImage();
        }
        


        
        

        userDao.updateUser(user_name,"null",birthday,sex,city,signature,college,time,cpProfile,"null","0",fn);
        String contextPath = request.getContextPath();
        try {
            pic.transferTo(targetFile);
            return new RedirectView(contextPath + "/pCenter/"+user_name);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
